package com.tcd.policy.service.impl;

import com.tcd.Policy.pojo.WarrantyInfo;
import com.tcd.policy.mapper.InsureRiskInfoMapper;
import com.tcd.policy.mapper.RecognizeeInfoMapper;
import com.tcd.policy.mapper.WarrantyInfoMapper;
import com.tcd.policy.service.WarrantyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 保单信息表 服务实现类
 * </p>
 *
 * @author cz
 * @since 2021-05-25
 */
@Service
public class WarrantyInfoServiceImpl implements WarrantyInfoService {

    @Autowired
    WarrantyInfoMapper mapper;
    @Autowired
    RecognizeeInfoMapper recognizeeInfoMapper;
    @Autowired
    InsureRiskInfoMapper insureRiskInfoMapper;

    @Override
    public List<WarrantyInfo> getWarrantyInfo(Integer pageIndex, Integer pageSize,String warrantyId, String licenseId, String policyholdersName, String insuranceType) {
        String policyholderId = null;
        if (policyholdersName != "" && policyholdersName != null && policyholdersName != "null") {
            policyholderId = recognizeeInfoMapper.selectrecognizeeInfoByName(policyholdersName);
        }
        return mapper.getWarrantyInfo(pageIndex,pageSize,warrantyId, licenseId, policyholderId, insuranceType);
    }

    //交强险
    @Override
    public int addWarrantyCompulsory(WarrantyInfo warrantyInfo) {
        List<WarrantyInfo> list = mapper.getWarrantyInfo(0,0,null, warrantyInfo.getLicenseId(),null, warrantyInfo.getInsuranceType().toString());
        int row1 = 0;
        if (list.size() > 0) {
            row1 = 0;
        } else {
            int row = mapper.addWarrantyCompulsory(warrantyInfo);
            if (row > 0) {
                row1 = insureRiskInfoMapper.addinsureRiskInfo(warrantyInfo.getWarrantyId());
            }
        }
        return row1;
    }

    //商业险
    @Override
    public int addWarrantyBusiness(WarrantyInfo warrantyInfo) {
        List<WarrantyInfo> list = mapper.getWarrantyInfo(0,0,null, warrantyInfo.getLicenseId(),null, warrantyInfo.getInsuranceType().toString());
        int row1 = 0;
        if (list.size() > 0) {
            row1 = 0;
        } else {
            int row = mapper.addWarrantyCompulsory(warrantyInfo);
            if (row > 0) {
                for (String item : warrantyInfo.getRiskTypeList()) {
                    row1 = insureRiskInfoMapper.addWarrantyBusiness(warrantyInfo.getWarrantyId(), item);
                }
            }
        }
        return row1;
    }

    //查询保单商业险关系
    @Override
    public List<String> selectInsureRiskInfo(String warrantyId) {
        return mapper.selectInsureRiskInfo(warrantyId);
    }

    //修改保单
    @Override
    public int updatePolicyByid(WarrantyInfo warrantyInfo) {
        int now = insureRiskInfoMapper.deleteInsureRiskInfoByid(warrantyInfo.getWarrantyId());
        int row1 = 0;
        int row2 = 0;
        if (now > 0) {
            for (String item : warrantyInfo.getRiskTypeList()) {
                row1 = insureRiskInfoMapper.addWarrantyBusiness(warrantyInfo.getWarrantyId(), item);
            }
        }
        if (row1 > 0) {
            row2 = mapper.updatePolicyByid(warrantyInfo);
        }
        return row2;
    }

    //修改交强险保单
    @Override
    public int updatePolicyCompulsoryByid(WarrantyInfo warrantyInfo) {
        return mapper.updatePolicyByid(warrantyInfo);
    }

    //查询未审核的保单
    @Override
    public List<WarrantyInfo> getWarrantyInfoByauditStatus(String warrantyId, String licenseId, String policyholdersName) {
        String policyholderId = null;
        if (policyholdersName != "" && policyholdersName != null && policyholdersName != "null") {
            policyholderId = recognizeeInfoMapper.selectrecognizeeInfoByName(policyholdersName);
        }
        return mapper.getWarrantyInfoByauditStatus(warrantyId, licenseId, policyholderId);
    }

    @Override
    public int updateAuditStatus(String auditStatus, String warrantyId) {
        return mapper.updateAuditStatus(auditStatus, warrantyId);
    }

    @Override
    public int updateaccidentsNumById(String[] warrantyId) {
        return mapper.updateaccidentsNumById(warrantyId);
    }

    //查询保单总数
    @Override
    public Integer getWarrantyInfoCount(String warrantyId, String licenseId, String policyholdersName) {
        String policyholderId = null;
        if (policyholdersName != "" && policyholdersName != null && policyholdersName != "null") {
            policyholderId = recognizeeInfoMapper.selectrecognizeeInfoByName(policyholdersName);
        }
        return mapper.getWarrantyInfoCount(warrantyId,licenseId,policyholderId);
    }

}
